**********************************************************************************	
*** TABLE A3: Hearing Incidence and Improper Payment Rate By Lag Amount and Rates
**********************************************************************************
use "$DATALOC/AgencyLevel.dta", clear
tsset agencyid FY
qui g log_ip_dollars = log(improperpaymentamountm)
qui g log_ip_rate = log(iprate)
forvalues i=1/3 {
	qui g L`i'_log_ip_dollars = L`i'.log_ip_dollars
	label var L`i'_log_ip_dollars "Log IP amount, t-`i'"
	qui g L`i'_log_ip_rate = L`i'.log_ip_rate
	label var L`i'_log_ip_rate "Log IP rate, t-`i'"
}
label var agen_hear_all "Hearing"
label var iprate "IP rate"


* Lag IP rate.
eststo s1_1: reghdfe agen_hear_all L1_log_ip_rate, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	
eststo s1_2: reghdfe agen_hear_all L*_log_ip_rate, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	

* Lag improper payment dollars.
eststo s1_3: reghdfe agen_hear_all L1_log_ip_dollars, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	
eststo s1_4: reghdfe agen_hear_all L*_log_ip_dollars, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	

* Lag rate vs dollars.
eststo s1_5: reghdfe agen_hear_all L1_log_ip_rate L1_log_ip_dollars, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	
eststo s1_6: reghdfe agen_hear_all L*_log_ip_rate L*_log_ip_dollars, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	

* Conditional on having had a first hearing.
qui g hearingXyear = agen_hear_all * FY
qui replace hearingXyear = 99999 if hearingXyear == 0
qui egen first_hearing = min(hearingXyear), by(agency)
qui g has_had_hearing = FY >= first_hearing & first_hearing != 0
tab first_hearing has_had_hearing 
* TWFE.
eststo s1_7: reghdfe agen_hear_all L1_log_ip_rate L1_log_ip_dollars if has_had_hearing == 1, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	quietly estadd local conditional "Yes", replace
	estadd ysumm, replace	
eststo s1_8: reghdfe agen_hear_all L*_log_ip_rate L*_log_ip_dollars if has_had_hearing == 1, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	quietly estadd local conditional "Yes", replace
	estadd ysumm, replace	

* Original model controlling for lag ip selection effects.
reghdfe iprate agen_hear_all last_agen_hear L1.log_ip_rate L1.log_ip_dollars,  a(agency FY)
eststo s1_10: reghdfe iprate agen_hear_all last_agen_hear L*_log_ip_rate L*_log_ip_dollars, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	
* Original model on same sample.
qui g _the_sample = e(sample)
eststo s1_9: reghdfe iprate agen_hear_all last_agen_hear if _the_sample, a(agency FY)
	quietly estadd local fixedyear "Yes", replace
	quietly estadd local fixedagency "Yes", replace
	estadd ysumm, replace	
drop _the_sample

* Table.
esttab s1_1 s1_2 s1_3 s1_4 s1_5 s1_6 s1_7 s1_8 s1_9 s1_10 using "$TABLELOC/TableA03.tex", ///
	prehead("\begin{sidewaystable}[htbp]\centering \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \caption{Hearing Incidence and Improper Payment Rate By Lag Amounts and Rates.} \resizebox{.9\textwidth}{!}{ \begin{tabular}{l*{11}{c}} \toprule") ///
	b(a2) fragment ///
	booktabs label se star(** 0.05 *** 0.01) s(fixedyear fixedagency conditional N r2 r2_within, label("Fiscal Year FE" "Agency/Program FE" "Had Previous Hearing" "Observations" "R squared" "Within R squared")) ///
	keep(*) ///
	nomtitles ///
	mgroups("Hearing incidence" "Agency IP rate", ///
				pattern(1 0 0 0 0 0 0 0 1 0) ///
				span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	prefoot("\hline") ///
	postfoot("\bottomrule \multicolumn{8}{l}{\footnotesize Standard errors in parentheses.}\\ \multicolumn{4}{l}{\footnotesize \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} } \\ \label{tab:pretrends} \end{sidewaystable}") replace
  
